#import "SMRShadowTextFieldCell.h"
#import "SMRShadowTextField.h"


@interface SMRShadowTextFieldCell (SMRShadowTextFieldCellPrivateMethods)
-(void)initializeCell_;
-(void)updateTextColor_;
@end


#pragma mark -
@implementation SMRShadowTextFieldCell
@synthesize editing;
@synthesize normalColor;
@synthesize highlightedColor;
@synthesize editingColor;


#pragma mark -
#pragma mark Initialization

-(id)initTextCell:(NSString *)aString {
	if (!(self = [super initTextCell:aString]))
		return nil;
	[self initializeCell_];
	return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self initializeCell_];
	return self;
}

-(void)initializeCell_ {
	[self setDrawsBackground:NO];
	[self setNormalColor:[NSColor blackColor]];
	[self setHighlightedColor:[NSColor whiteColor]];
	[self setEditingColor:[NSColor blackColor]];
}


#pragma mark -
#pragma mark Accessors

-(void)setEditing:(BOOL)shouldEdit {
	editing = shouldEdit;
	[self updateTextColor_];
}


#pragma mark -
#pragma mark Drawing

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	if ([self editing]) {
		NSRect overlayRect = [(SMRShadowTextField *)controlView overlayInnerRect];
		[[NSColor whiteColor] setFill];
		[NSBezierPath fillRect:overlayRect];
	}
	[self drawInteriorWithFrame:cellFrame inView:controlView];
}


#pragma mark -
#pragma mark NSTextFieldCell overrides

-(void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
	[super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
	[(SMRShadowTextField *)[self controlView] showEditModeBezel];
	[self setEditing:YES];
}

-(void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
	[super editWithFrame:aRect inView:controlView editor:textObj delegate:anObject event:theEvent];
	[(SMRShadowTextField *)[self controlView] showEditModeBezel];
	[self setEditing:YES];
}

-(void)setObjectValue:(id<NSCopying>)obj {
	[super setObjectValue:obj];
	[(SMRShadowTextField *)[self controlView] updateOverlay];
}

-(NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
	return nil;
}

-(void)setHighlighted:(BOOL)shouldHighlight {
	[super setHighlighted:shouldHighlight];
	[self updateTextColor_];
}


#pragma mark -
#pragma mark Private

-(void)updateTextColor_ {
	NSColor *normalTextColor = [self isHighlighted] ? [self highlightedColor] : [self normalColor];
	[self setTextColor:[self editing] ? [self editingColor] : normalTextColor];
}

@end
